package com.hhh.demo.sx.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.demo.sx.bean.Role;
import com.hhh.demo.sx.service.RoleService;
import com.hhh.demo.sx.service.UserRoleService;
import com.hhh.demo.sx.util.ResultMap;
import com.hhh.demo.sx.util.ResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hhh
 * @since 2021-12-30
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleService userRoleService;

    /**
     * 打开角色主页面
     */
    @RequestMapping("/index")
    public String openPage() {
        return "role";
    }

    /**
     * 查询角色列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> getList(@RequestParam("page") int p,
                                       @RequestParam("limit") int limit,
                                       @RequestParam(value = "condition", required = false) String condition) {
        IPage<Role> Page = null;
        QueryWrapper<Role> warpper = new QueryWrapper<>();
        Page<Role> pages = new Page<>(p, limit);
        ;
        JSONObject jsonObject = JSONObject.parseObject(condition);
        if (jsonObject != null) {
            String username = (String) jsonObject.get("name");
            warpper.like(!username.equals(""), "name", username);
            Page = roleService.findAll(pages, warpper);
        } else {
            Page = roleService.findAll(pages, null);
        }


        return ResultPage.Page(Page.getRecords(), Page.getTotal());

    }

    /**
     * 新增/修改角色
     */
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public ResultMap saveOrUpdate(Role role) {
        boolean f = roleService.saveOrUpdate(role);

        if (f) {
            return ResultMap.success();
        } else {
            return ResultMap.fail();
        }
    }

    /**
     * 查找角色
     */
    @ResponseBody
    @RequestMapping("/getOne")
    public ResultMap getOne(int id) {
        Role role = roleService.getById(id);
        return ResultMap.success(role);
    }

    /**
     * 删除角色
     */
    @Transactional
    @ResponseBody
    @RequestMapping("/delete")
    public ResultMap delete(int id) {
        Integer f = roleService.doDel(id);
        if (f == 0) {
            return ResultMap.success();
        } else if (f == 1) {
            return new ResultMap(500, "删除失败！");
        } else if (f == 2) {
            return new ResultMap(500, "删除失败！有用户正在使用这个角色！");
        } else {
            return ResultMap.fail();
        }
    }

    /**
     * 获得所有角色
     */
    @ResponseBody
    @RequestMapping("/getALL")
    public ResultMap getALL() {
        return ResultMap.success(roleService.list());
    }
}

